
package com.scalar.chiptrack;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Wrapper around the STRUTS Controller Servlet.
 * Used to perform any required operations before forwarding
 * the request to the actual Business Object (ACTION class)
 *
 * Right now - for each request - ChipTrac Controller checks
 * for user validity
 *
 * @author Balaram Putta
 * @version
 */
public class ChipTracControllerServlet extends org.apache.struts.action.ActionServlet
{
    private static final String LOGIN_PATH = "login.do";
    private static final String LOGOFF_PATH = "logoff.do";
    private static final String GLOBAL_LOGIN_FORWARD_PATH = "login";

    /**
     * Calls super class' init() method to load
     * all action and form-bean mappings from /WEB-INF/Struts-Config.xml file
     */
    public void init() throws javax.servlet.ServletException
    {
        super.init();
        System.out.println( "ChipTrac Controller - Initialized" );
    }

    /**
     * Overwrites to doGet() of base class to check for User validity
     */
    public void doGet ( HttpServletRequest request, HttpServletResponse response )
        throws IOException, ServletException
    {

        // Step 1
        // Process ChipTrac checks
        processRequest ( request, response );

        // Step 2
        // Send it to the STRUTS Controller for actual processing
        super.doGet ( request, response );
    }


    /**
     * Overwrites to doPost() of base class to check for User validity
     */
    public void doPost ( HttpServletRequest request, HttpServletResponse response )
        throws IOException, ServletException
    {
        // Step 1
        // Process ChipTrac checks
        processRequest ( request, response );

        // Step 2
        // Send it to the STRUTS Controller for actual processing
        super.doPost ( request, response );
    }

    public void destroy()
    {
        System.out.println( "ChipTrac Controller - has been stopped" );
    }

    /**
     * Checks whether the user is logged in or not.
     * If not - then this method forwards the user to login page.
     * Also it sets the return path.
     */
    private void processRequest (HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        /*
        String requestURI = request.getServletPath();
        String queryString = request.getQueryString();
        String returnPath = requestURI;

        if( ( queryString != null ) && ( queryString.trim().length() != 0 ) )
        {
            returnPath = returnPath + "?" + queryString;
        }

        if( ( requestURI.indexOf (LOGIN_PATH) == -1 ) &&      // Check for user login, only
            ( requestURI.indexOf (LOGOFF_PATH) == -1 ) )      // if the request is not Login or Logout
        {
            HttpSession session = request.getSession();
            User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );

            if ( user == null )
            {
                System.out.println ( "ChipTrac Controller: User is not logged in, send it to login page" );
                System.out.println ( "ChipTrac Controller: After successful login: " + returnPath );

                session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, returnPath );
                getServletContext().getRequestDispatcher( getModuleConfig (request).findForwardConfig( GLOBAL_LOGIN_FORWARD_PATH ).getPath() ).forward( request, response );
            }
        }
        */
    }

}